package com.mobialia.trivial.es;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Random;

import android.content.Context;
import android.util.Log;

public class Questions {

	public static final int CATEGORY_ART             = 0;
	public static final int CATEGORY_LITERATURE      = 1;
	public static final int CATEGORY_MUSIC           = 2;
	public static final int CATEGORY_CINEMA          = 3;
	public static final int CATEGORY_SCIENCE         = 4;
	public static final int CATEGORY_PHYSICS_CHEMIST = 5;
	public static final int CATEGORY_BIOLOGY         = 6;
	public static final int CATEGORY_HEALTH          = 7;
	public static final int CATEGORY_GEOGRAPHY       = 8;
	public static final int CATEGORY_HISTORY         = 9;
	public static final int CATEGORY_ECONOMY         = 10;
	public static final int CATEGORY_POLITICS        = 11;
	public static final int CATEGORY_SPORTS          = 12;
	public static final int CATEGORY_GASTRONOMY      = 13;
	public static final int CATEGORY_TV              = 14;
	public static final int CATEGORY_RANDOM          = 15;
	
	public static Integer[] CATEGORY_IMAGES = {
		R.drawable.art,
		R.drawable.literature,
		R.drawable.music,
		R.drawable.cinema,
		R.drawable.science,
		R.drawable.physics_chemistry,
		R.drawable.biology,
		R.drawable.health,
		R.drawable.geography,
		R.drawable.history,
		R.drawable.economy,
		R.drawable.politics,
		R.drawable.sports,
		R.drawable.gastronomy,
		R.drawable.tv,
		R.drawable.random
	};
	
	public static Integer[] CATEGORY_STRINGS = {
		R.string.category_art,
		R.string.category_literature,
		R.string.category_music,
		R.string.category_cinema,
		R.string.category_science,
		R.string.category_physics_chemistry,
		R.string.category_biology,
		R.string.category_health,
		R.string.category_geography,
		R.string.category_history,
		R.string.category_economy,
		R.string.category_politics,
		R.string.category_sports,
		R.string.category_gastronomy,
		R.string.category_tv,
		R.string.category_random
	};
	
//	public static Bitmap bitmaps[];

//	static {
//		bitmaps = new Bitmap[CATEGORY_IMAGES.length];
//		for (int i = 0; i < CATEGORY_IMAGES.length; i++) {
//			bitmaps[i] = Bitmap.c
//		}
//	}
	
	int category;
	String question;
	
	void nextQuestion(Context context, int cat) {
		category = cat;
		
		Random r = new Random(System.currentTimeMillis());

		if (category == CATEGORY_RANDOM) {
			category = (int) Math.floor(r.nextFloat() * (CATEGORY_STRINGS.length-1));	
		}
		
		String questionFile = "";
		int questionCount = 0;

		switch(category) {
		case CATEGORY_ART:
			questionFile = "Arte.txt";
			questionCount = 4114;
			break;
		case CATEGORY_BIOLOGY:
			questionFile = "Biologia.txt";
			questionCount = 6805;
			break;
		case CATEGORY_SCIENCE:
			questionFile = "Ciencia.txt";
			questionCount = 5212;
			break;
		case CATEGORY_CINEMA:
			questionFile = "Cine.txt";
			questionCount = 17337;
			break;
		case CATEGORY_TV:
			questionFile = "Cotilleo-TV.txt";
			questionCount = 5365;
			break;
		case CATEGORY_SPORTS:
			questionFile = "Deporte.txt";
			questionCount = 13528;
			break;
		case CATEGORY_ECONOMY:
			questionFile = "Economia.txt";
			questionCount = 2040;
			break;
		case CATEGORY_PHYSICS_CHEMIST:
			questionFile = "Fisica-Quimica.txt";
			questionCount = 3570;
			break;
		case CATEGORY_GASTRONOMY:
			questionFile = "Gastronomia.txt";
			questionCount = 2251;
			break;
		case CATEGORY_GEOGRAPHY:
			questionFile = "Geografia.txt";
			questionCount = 10692;
			break;
		case CATEGORY_HISTORY:
			questionFile = "Historia.txt";
			questionCount = 7306;
			break;
		case CATEGORY_LITERATURE:
			questionFile = "Literatura.txt";
			questionCount = 7985;
			break;
		case CATEGORY_HEALTH:
			questionFile = "Medicina-Salud.txt";
			questionCount = 4219;
			break;
		case CATEGORY_MUSIC:
			questionFile = "Musica.txt";
			questionCount = 5479;
			break;
		case CATEGORY_POLITICS:
			questionFile = "Politica.txt";
			questionCount = 3304;
			break;
		}
		
		int questionNumber = (int) Math.floor(r.nextFloat() * questionCount);
		if ((category == CATEGORY_CINEMA || category == CATEGORY_SPORTS) && questionNumber > 8000) {
			questionNumber -= 8000;
			questionFile = questionFile + "2";
		}
		
		Log.i("Question", questionFile + ": " + questionNumber);
		try {
			InputStream is = context.getAssets().open(questionFile);
			BufferedReader reader = new BufferedReader(new InputStreamReader(is));
			// Skips lines
			for (int i = 0; i< questionNumber; i++) {
				reader.readLine();
			}
			question = reader.readLine();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public int getCategory() {
		return category;
	}
	
	public String getQuestion() {
//		return question.substring(question.indexOf("«") + 1, question.indexOf("*"));
		return question.substring(0, question.indexOf("*"));
	}
	
	public String getAnswer() {
		return question.substring(question.indexOf("*") + 1);
	}
}
